package cn.rkylin.oms.system.project.service;

import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.context.ContextLoader;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;

import cn.rkylin.apollo.common.util.HttpUtils;
import cn.rkylin.apollo.common.util.SnoGerUtil;
import cn.rkylin.core.ApolloMap;
import cn.rkylin.core.ApolloRet;
import cn.rkylin.core.IDataBaseFactory;
import cn.rkylin.core.service.ApolloService;
import cn.rkylin.oms.common.context.CurrentUser;
import cn.rkylin.oms.common.context.WebContextFactory;
import cn.rkylin.oms.common.jobs.AbstractJob;
import cn.rkylin.oms.system.project.dao.IProjectManageDAO;
import cn.rkylin.oms.system.project.vo.MdmNonSettleRecodes;
import cn.rkylin.oms.system.project.vo.MdmProjectVO;
import cn.rkylin.oms.system.project.vo.MdmSettleRecodes;
import cn.rkylin.oms.system.project.vo.ProjectVO;


@Service
public class ProjectManagerService extends ApolloService {
    @Autowired
    private IDataBaseFactory dao;
    
    @Autowired
    private IProjectManageDAO projectManageDAO;
    
    @Autowired
    public ApolloService commonService;

    @Value("${mdm.server.url}")
    private String mdmServerUrl;

    @Value("${mdm.server.security}")
    private String mdmServerSecurity;

    @Value("${mdm.server.appId}")
    private String mdmServerAppId;

	// 调度服务器的url地址，见app.properties
	@Value("${executor.addProject.url}")
	protected String addProject;
	
	@Value("${executor.updateProject.url}")
	protected String updateProject;
	
    public PageInfo<ProjectVO> findSplitProjects(int page, int rows, ProjectVO prjVO) throws Exception {
        PageInfo<ProjectVO> prjVOList = findPage(page, rows, "pageSelectSplitProject", prjVO);
        return prjVOList;
    }
    
    public PageInfo<ProjectVO> findByWhere(int page, int rows, ProjectVO prjVO) throws Exception {
        PageInfo<ProjectVO> prjVOList = findPage(page, rows, "pageSelectProject", prjVO);
        return prjVOList;
    }

    @CachePut(value = "project", key = "T(String).valueOf('project:').concat(#params['prjId'])")
    public ProjectVO insertProject(ProjectVO params) throws Exception {
        int r = projectManageDAO.insert(params);
        if (r != 1) {
            return null;
        }
        
        /** 暂时不需要，因为是淘宝试用该验证 **/
        /*Map<String, String> postDatas = new HashMap<String, String>();
        postDatas.put("code.codekey", "JOB_GROUP");
        postDatas.put("code.keycode", params.getPrjId());
        postDatas.put("code.keyname", params.getPrjName());
        postDatas.put("code.keydesc", params.getRemark());
        try {
            HttpUtils.postMethod(addProject, postDatas);
        } catch (Exception ex) {
            throw new Exception("作业组创建失败", ex);
        }*/
        return params;
    }

    @SuppressWarnings("unchecked")
	public void initProject(String prjId) throws Exception {
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("prjId", prjId);
		PageInfo<ApolloRet> pageList = dao.findByPage(1, 1, "getProjectInfo", paramsMap);
		Map projectMap = pageList.getList().get(0);
		ProjectVO params = new ProjectVO();
		params.setPrjId(prjId);
		params.setPrjName((String)projectMap.get("prj_name"));
        
		// 库存结余作业(定时任务)
		AbstractJob stockBalanceJob = (AbstractJob) ContextLoader.getCurrentWebApplicationContext()
				.getBean("stockBalanceJob");
		stockBalanceJob.createJob(params);

		// 奇门7个定时任务
		AbstractJob qmCancleConsign = (AbstractJob) ContextLoader.getCurrentWebApplicationContext()
				.getBean("qmCancleConsign");
		qmCancleConsign.createJob(params);

		AbstractJob qmCreateConsign = (AbstractJob) ContextLoader.getCurrentWebApplicationContext()
				.getBean("qmCreateConsign");
		qmCreateConsign.createJob(params);

		AbstractJob qmCreateOtherStockOut = (AbstractJob) ContextLoader.getCurrentWebApplicationContext()
				.getBean("qmCreateOtherStockOut");
		qmCreateOtherStockOut.createJob(params);

		AbstractJob qmCreateReturnStockIn = (AbstractJob) ContextLoader.getCurrentWebApplicationContext()
				.getBean("qmCreateReturnStockIn");
		qmCreateReturnStockIn.createJob(params);

		AbstractJob qmCreateStockIn = (AbstractJob) ContextLoader.getCurrentWebApplicationContext()
				.getBean("qmCreateStockIn");
		qmCreateStockIn.createJob(params);

		AbstractJob qmProductSync = (AbstractJob) ContextLoader.getCurrentWebApplicationContext()
				.getBean("qmProductSync");
		qmProductSync.createJob(params);

		AbstractJob qmStockInventory = (AbstractJob) ContextLoader.getCurrentWebApplicationContext()
				.getBean("qmStockInventory");
		qmStockInventory.createJob(params);
    }
    
    @CacheEvict(value = "project", key = "T(String).valueOf('project:').concat(#params['prjId'])")
    public int updateProject(ApolloMap<String, Object> params) throws Exception {
        int r = dao.update("updatePrj", params);
        
        /** 暂时不需要，因为是淘宝试用该验证 **/
        /*Map<String, String> postDatas = new HashMap<String, String>();
        postDatas.put("code.codekey", "JOB_GROUP");
        postDatas.put("code.keycode", (String)params.get("prjId"));
        postDatas.put("code.keyname", (String)params.get("prjName"));
        postDatas.put("code.keydesc", (String)params.get("remark"));
        HttpUtils.postMethod(addProject, postDatas);*/
        
        return r;
    }

    @CacheEvict(value = "project", key = "T(String).valueOf('project:').concat(#params['prjId'])")
    public int updatePrjEnable(ApolloMap<String, Object> params) throws Exception {
        int r = dao.update("updatePrjEnable", params);
        return r;
    }

    /**
     * 接受主动发过来的数据变更
     * @param json
     * @param type
     * @return
     * @throws Exception
     */
    public Object receiveMdm(String json,String type) throws Exception {
//        List<MdmSettleRecodes> listRecodes = new ArrayList<MdmSettleRecodes>();
        List<String> listRecodesJson = JSON.parseArray(json, String.class);
//        listRecodes = JSON.parseArray(json, MdmSettleRecodes.class);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (listRecodesJson!=null && listRecodesJson.size()>0){
            for (int i = 0 ; i < listRecodesJson.size() ; i++){
                MdmSettleRecodes mdmSettleRecodes = JSON.parseObject(listRecodesJson.get(i),MdmSettleRecodes.class);
                ProjectVO projectVO = new ProjectVO();
                projectVO.setPrjCode(mdmSettleRecodes.getProject_code());
                projectVO.setPrjName(mdmSettleRecodes.getProject_name());
                projectVO.setPrjCategory(mdmSettleRecodes.getProject_category());
                projectVO.setMdmCode(mdmSettleRecodes.getMdm_code());
                projectVO.setMdmResponse(listRecodesJson.get(i));
                //暂时不要对应删除
//                if ("3".equals(type)){
//                    projectVO.setDeleted("y");
//                } else {
//                    projectVO.setDeleted("n");
//                }
                if (StringUtils.isNotBlank(mdmSettleRecodes.getEnd_time())){
                    projectVO.setExpireTime(df.parse(mdmSettleRecodes.getEnd_time()));
                }
                projectVO.setCons(mdmSettleRecodes.getPerson_in_charge());
                projectVO.setTel(mdmSettleRecodes.getPerson_in_charge_tel());
                projectVO.setRemark(mdmSettleRecodes.getRemark());
                projectVO.setBrandCode(mdmSettleRecodes.getBrand_code());
                projectVO.setBrand(mdmSettleRecodes.getBrand());

//                ApolloMap<String, Object> params = new ApolloMap<String, Object>();
//                params.put("prjId", mdmSettleRecodes.getProject_code());
//                PageInfo<ApolloRet> list = dao.findByPage(1, 1, "getProjectInfo", params);

                ProjectVO vo = (ProjectVO) dao.findOne("selectByMdmCodePrj", mdmSettleRecodes.getProject_code());
                if (vo!=null) {
                    //更新
                    projectVO.setPrjId(vo.getPrjId());
                    projectManageDAO.update(projectVO);
                } else {
                    //插入
                    projectVO.setEntId("51f17f6ddbae11e68c4d001e67c7ec3a");
                    projectVO.setEntName("瑞金麟集团");
                    projectVO.setPrjType("其他");
                    projectVO.setPrjShortName(java.util.UUID.randomUUID().toString().replace("-", "").substring(0,4).toUpperCase());//随机生成4位，然后以后人工修改
                    projectVO.setPrjId(SnoGerUtil.getUUID());
                    projectVO.setEnable("n");
                    projectManageDAO.insert(projectVO);
                    
                    Map<String, String> postDatas = new HashMap<String, String>();
                    postDatas.put("code.codekey", "JOB_GROUP");
                    postDatas.put("code.keycode", projectVO.getPrjId());
                    postDatas.put("code.keyname", projectVO.getPrjName());
                    postDatas.put("code.keydesc", projectVO.getRemark());
                    HttpUtils.postMethod(addProject, postDatas);
                }
            }
//            return listRecodes;
        }
        return null;
    }

    /**
     * 接受主动发过来的数据变更
     * @param json
     * @param type
     * @return
     * @throws Exception
     */
    public Object receiveMdmNon(String json,String type) throws Exception {
        List<MdmNonSettleRecodes> listRecodes = new ArrayList<MdmNonSettleRecodes>();
        List<String> listRecodesJson = JSON.parseArray(json, String.class);
//        listRecodes = JSON.parseArray(json, MdmNonSettleRecodes.class);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (listRecodesJson!=null && listRecodesJson.size()>0){
            for (int i = 0 ; i < listRecodesJson.size() ; i++){
                MdmNonSettleRecodes mdmNonSettleRecodes = JSON.parseObject(listRecodesJson.get(i),MdmNonSettleRecodes.class);
                ProjectVO projectVO = new ProjectVO();
                projectVO.setPrjCode(mdmNonSettleRecodes.getProject_code());
                projectVO.setPrjName(mdmNonSettleRecodes.getProject_name());
                projectVO.setPrjCategory(mdmNonSettleRecodes.getProject_category());
                projectVO.setMdmCode(mdmNonSettleRecodes.getMdm_code());
                projectVO.setMdmResponse(listRecodesJson.get(i));
                //暂时不要对应删除
//                if ("3".equals(type)){
//                    projectVO.setDeleted("y");
//                } else {
//                    projectVO.setDeleted("n");
//                }
                if (StringUtils.isNotBlank(mdmNonSettleRecodes.getEnd_time())){
                    projectVO.setExpireTime(df.parse(mdmNonSettleRecodes.getEnd_time()));
                }
                projectVO.setCons(mdmNonSettleRecodes.getPerson_in_charge());
                projectVO.setTel(mdmNonSettleRecodes.getPerson_in_charge_tel());
                projectVO.setRemark(mdmNonSettleRecodes.getRemark());

                ProjectVO vo = (ProjectVO) dao.findOne("selectByMdmCodePrj", mdmNonSettleRecodes.getProject_code());
                if (vo!=null) {
                    //更新
                    projectVO.setPrjId(vo.getPrjId());
                    projectManageDAO.update(projectVO);
                } else {
                    //插入
                    projectVO.setEntId("51f17f6ddbae11e68c4d001e67c7ec3a");
                    projectVO.setEntName("瑞金麟集团");
                    projectVO.setPrjType("其他");
                    projectVO.setPrjShortName(java.util.UUID.randomUUID().toString().replace("-", "").substring(0,4).toUpperCase());//随机生成4位，然后以后人工修改
                    projectVO.setEnable("n");
                    projectVO.setPrjId(SnoGerUtil.getUUID());
                    projectManageDAO.insert(projectVO);
                    
                    Map<String, String> postDatas = new HashMap<String, String>();
                    postDatas.put("code.codekey", "JOB_GROUP");
                    postDatas.put("code.keycode", projectVO.getPrjId());
                    postDatas.put("code.keyname", projectVO.getPrjName());
                    postDatas.put("code.keydesc", projectVO.getRemark());
                    HttpUtils.postMethod(addProject, postDatas);
                }
            }
//            return listRecodes;
        }
        return null;
    }
    /**
     * 下载主数据的项目数据
     * @return
     * @throws Exception
     */
    public Map<String, String> downProject() throws Exception {
        Map<String, String> map = downNonSettlementDown("1",0);
        if ("y".equals(map.get("success"))){
            map = downSettlementDown("1",0);
        }
        return  map;
    }

    /**
     * 查询非结算类项目
     * @return
     */
    private Map<String, String> downNonSettlementDown(String page,int rows) throws Exception {
        Map<String, String> resultMap = new HashMap<String, String>();

        Map<String, Object> dataValueMap = new HashMap<String, Object>();
        dataValueMap.put("system_code","ERP001");
        dataValueMap.put("page",new Integer(page));
        dataValueMap.put("page_size",new Integer("1000"));
        String data = "{\"system_code\":\"ERP001\",\"page\":"+page+",\"page_size\":\"1000\"}";

        String t = String.valueOf((new Date()).getTime());
        String sign = MD5(mdmServerSecurity + t + data);

        StringBuffer sb = new StringBuffer(); //查询结算类项目
        sb.append(mdmServerUrl+ "/api/mdm_non_settlement_project/query" + "?");
        sb.append("appId=" + mdmServerAppId );
        sb.append("&timestamp=" + t);
        sb.append("&sign=" + sign);
        sb.append("&system_code=ERP001&page="+page+"&page_size=1000");
//        sb.append("&data=" + data);

//        String json = HttpUtils.sendByGet(sb.toString(), null);
        String json = HttpUtils.sendByPost(sb.toString(), data);
        System.out.println(json);

        MdmProjectVO mdmProjectVO = JSON.parseObject(json, MdmProjectVO.class);
        List<String> listRecodesJson = JSON.parseArray(mdmProjectVO.getResult().getData().getRecords(), String.class);
//        List<MdmNonSettleRecodes> listRecodes = JSON.parseArray(mdmProjectVO.getResult().getData().getRecords(), MdmNonSettleRecodes.class);

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if (mdmProjectVO!=null && "S1".equals(mdmProjectVO.getStatus())){
            if (mdmProjectVO.getResult()!=null && "Y1".equals(mdmProjectVO.getResult().getStatus())){
                if (listRecodesJson!=null && listRecodesJson.size()>0){
                    Map<String ,String> map = new HashMap<String,String>();
                    for (int i = 0 ; i < listRecodesJson.size() ; i++){
                        MdmNonSettleRecodes mdmNonSettleRecodes = JSON.parseObject(listRecodesJson.get(i), MdmNonSettleRecodes.class);

                        ProjectVO projectVO = new ProjectVO();
                        projectVO.setPrjCode(mdmNonSettleRecodes.getProject_code());
                        projectVO.setPrjName(mdmNonSettleRecodes.getProject_name());
                        projectVO.setPrjCategory(mdmNonSettleRecodes.getProject_category());
                        projectVO.setMdmCode(mdmNonSettleRecodes.getMdm_code());
                        projectVO.setMdmResponse(listRecodesJson.get(i));
                        if (StringUtils.isNotBlank(mdmNonSettleRecodes.getEnd_time())){
                            projectVO.setExpireTime(df.parse(mdmNonSettleRecodes.getEnd_time()));
                        }

                        ProjectVO vo = (ProjectVO) dao.findOne("selectByMdmCodePrj", mdmNonSettleRecodes.getProject_code());
                        if (map.get(mdmNonSettleRecodes.getProject_code())==null){
                            map.put(mdmNonSettleRecodes.getProject_code(),mdmNonSettleRecodes.getProject_code());
                        } else {
                            System.out.println(mdmNonSettleRecodes.getProject_code());
                        }

                        if (vo!=null) {
                            // 更新
                            projectVO.setPrjId(vo.getPrjId());
                            projectManageDAO.update(projectVO);
                        } else {
                            // 插入
                            // 是否启用、简称、类型只有新增的时候才创建
                            projectVO.setEntId("51f17f6ddbae11e68c4d001e67c7ec3a");
                            projectVO.setEntName("瑞金麟集团");
                            projectVO.setEnable("n");
                            projectVO.setPrjShortName(java.util.UUID.randomUUID().toString().replace("-", "").substring(0,4).toUpperCase());//随机生成4位，然后以后人工修改
                            projectVO.setPrjType("其他");
                            projectVO.setPrjId(SnoGerUtil.getUUID());
                            projectManageDAO.insert(projectVO);
                            
                            Map<String, String> postDatas = new HashMap<String, String>();
                            postDatas.put("code.codekey", "JOB_GROUP");
                            postDatas.put("code.keycode", projectVO.getPrjId());
                            postDatas.put("code.keyname", projectVO.getPrjName());
                            postDatas.put("code.keydesc", projectVO.getRemark());
                            HttpUtils.postMethod(addProject, postDatas);
                        }
                    }
                    rows = rows + listRecodesJson.size();
                    if (Integer.valueOf(mdmProjectVO.getResult().getData().getTotalCount())- rows > 0){
                        Map<String,String> returnMap = downNonSettlementDown(String.valueOf(Integer.valueOf(page)+1),rows);
                        rows =  rows + Integer.valueOf(returnMap.get("size"));
                        resultMap.put("msg",returnMap.get("msg"));
                    }
                }
            } else if(mdmProjectVO.getResult()!=null && "Y2".equals(mdmProjectVO.getResult().getStatus())){
                resultMap.put("size","0");
                resultMap.put("msg","参数错误");
            } else if(mdmProjectVO.getResult()!=null && "Y3".equals(mdmProjectVO.getResult().getStatus())){
                resultMap.put("size","0");
                resultMap.put("msg","唯一性校验失败");
            } else if(mdmProjectVO.getResult()!=null && "Y4".equals(mdmProjectVO.getResult().getStatus())){
                resultMap.put("size","0");
                resultMap.put("msg","数据状态异常");
            }
        } else if(mdmProjectVO!=null && "S2".equals(mdmProjectVO.getStatus())){
            resultMap.put("size","0");
        } else if(mdmProjectVO!=null && "S3".equals(mdmProjectVO.getStatus())){
            resultMap.put("size","0");
        } else if(mdmProjectVO!=null && "S4".equals(mdmProjectVO.getStatus())){
            resultMap.put("size","0");
        } else {

        }
        if (mdmProjectVO!=null && resultMap.get("msg")==null){
            resultMap.put("msg",mdmProjectVO.getMessage());
        }
        resultMap.put("size",String.valueOf(rows));
        if (Integer.valueOf(mdmProjectVO.getResult().getData().getTotalCount())- rows > 0){
            resultMap.put("success","n");
        } else {
            resultMap.put("success","y");
        }
        return resultMap;
    }

    /**
     * 查询结算类项目
     * @return
     */
    private Map<String, String> downSettlementDown(String page,int rows) throws Exception {
        Map<String, String> resultMap = new HashMap<String, String>();
        String data = "{\"system_code\":\"ERP001\",\"page\":"+page+",\"page_size\":\"1000\"}";
        String t = String.valueOf((new Date()).getTime());
        String sign = MD5(mdmServerSecurity + t + data);

        StringBuffer sb = new StringBuffer(); //查询结算类项目
        sb.append(mdmServerUrl+ "/api/mdm_settlement_project/query" + "?");
        sb.append("appId=" + mdmServerAppId );
        sb.append("&timestamp=" + t);
        sb.append("&sign=" + sign);
        sb.append("&system_code=ERP001&page="+page+"&page_size=1000");
//        sb.append("&data=" + data);

        String json = HttpUtils.sendByPost(sb.toString(), data);//HttpUtils.sendByGet(sb.toString(), null);
        System.out.println(json);

        MdmProjectVO mdmProjectVO = JSON.parseObject(json, MdmProjectVO.class);

        List<String> listRecodesJson = JSON.parseArray(mdmProjectVO.getResult().getData().getRecords(), String.class);
//        List<MdmSettleRecodes> listRecodes = JSON.parseArray(mdmProjectVO.getResult().getData().getRecords(), MdmSettleRecodes.class);

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if (mdmProjectVO!=null && "S1".equals(mdmProjectVO.getStatus())){
            if (mdmProjectVO.getResult()!=null && "Y1".equals(mdmProjectVO.getResult().getStatus())){
                if (listRecodesJson!=null && listRecodesJson.size()>0){
                    Map<String ,String> map = new HashMap<String,String>();
                    for (int i = 0 ; i < listRecodesJson.size() ; i++){
                        MdmSettleRecodes mdmSettleRecodes = JSON.parseObject(listRecodesJson.get(i),MdmSettleRecodes.class);
                        ProjectVO projectVO = new ProjectVO();
                        projectVO.setPrjCode(mdmSettleRecodes.getProject_code());
                        projectVO.setPrjName(mdmSettleRecodes.getProject_name());
                        projectVO.setPrjCategory(mdmSettleRecodes.getProject_category());
                        projectVO.setMdmCode(mdmSettleRecodes.getMdm_code());
                        projectVO.setMdmResponse(listRecodesJson.get(i));
                        if (StringUtils.isNotBlank(mdmSettleRecodes.getEnd_time())){
                            projectVO.setExpireTime(df.parse(mdmSettleRecodes.getEnd_time()));
                        }
                        projectVO.setCons(mdmSettleRecodes.getPerson_in_charge());
                        projectVO.setTel(mdmSettleRecodes.getPerson_in_charge_tel());
                        projectVO.setRemark(mdmSettleRecodes.getRemark());
                        projectVO.setBrandCode(mdmSettleRecodes.getBrand_code());
                        projectVO.setBrand(mdmSettleRecodes.getBrand());

                        ProjectVO vo = (ProjectVO) dao.findOne("selectByMdmCodePrj", mdmSettleRecodes.getProject_code());
                        if (map.get(mdmSettleRecodes.getProject_code())==null){
                            map.put(mdmSettleRecodes.getProject_code(),mdmSettleRecodes.getProject_code());
                        } else {
                            System.out.println(mdmSettleRecodes.getProject_code());
                        }
                        if (vo!=null) {
                            //更新
                            projectVO.setPrjId(vo.getPrjId());
                            projectManageDAO.update(projectVO);
                        } else {
                            //插入
                            projectVO.setEntId("51f17f6ddbae11e68c4d001e67c7ec3a");
                            projectVO.setEntName("瑞金麟集团");
                            projectVO.setEnable("n");
                            projectVO.setPrjType("其他");
                            projectVO.setPrjShortName(java.util.UUID.randomUUID().toString().replace("-", "").substring(0,4).toUpperCase());//随机生成4位，然后以后人工修改
                            projectVO.setPrjId(SnoGerUtil.getUUID());
                            projectManageDAO.insert(projectVO);
                            
                            Map<String, String> postDatas = new HashMap<String, String>();
                            postDatas.put("code.codekey", "JOB_GROUP");
                            postDatas.put("code.keycode", projectVO.getPrjId());
                            postDatas.put("code.keyname", projectVO.getPrjName());
                            postDatas.put("code.keydesc", projectVO.getRemark());
                            HttpUtils.postMethod(addProject, postDatas);
                        }
                    }
                    rows = rows + listRecodesJson.size();
                    if (Integer.valueOf(mdmProjectVO.getResult().getData().getTotalCount())- rows > 0){
                        Map<String,String> returnMap = downSettlementDown(String.valueOf(Integer.valueOf(page)+1),rows);
                        rows =  rows + Integer.valueOf(returnMap.get("size"));
                        resultMap.put("msg",returnMap.get("msg"));
                    }
                }
            } else if(mdmProjectVO.getResult()!=null && "Y2".equals(mdmProjectVO.getResult().getStatus())){
                resultMap.put("size","0");
                resultMap.put("msg","参数错误");
            } else if(mdmProjectVO.getResult()!=null && "Y3".equals(mdmProjectVO.getResult().getStatus())){
                resultMap.put("size","0");
                resultMap.put("msg","唯一性校验失败");
            } else if(mdmProjectVO.getResult()!=null && "Y4".equals(mdmProjectVO.getResult().getStatus())){
                resultMap.put("size","0");
                resultMap.put("msg","数据状态异常");
            }
        } else if(mdmProjectVO!=null && "S2".equals(mdmProjectVO.getStatus())){
            resultMap.put("size","0");
        } else if(mdmProjectVO!=null && "S3".equals(mdmProjectVO.getStatus())){
            resultMap.put("size","0");
        } else if(mdmProjectVO!=null && "S4".equals(mdmProjectVO.getStatus())){
            resultMap.put("size","0");
        } else {

        }
        if (mdmProjectVO!=null && resultMap.get("msg")==null){
            resultMap.put("msg",mdmProjectVO.getMessage());
        }
        resultMap.put("size",String.valueOf(rows));
        if (Integer.valueOf(mdmProjectVO.getResult().getData().getTotalCount())- rows > 0){
            resultMap.put("success","n");
        } else {
            resultMap.put("success","y");
        }
        return resultMap;
    }

    private static String MD5(String message) {
        String mess = message;
        char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        try {
            byte[] btInput = mess.getBytes("UTF-8");
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要
            mdInst.update(btInput);
            // 获得密文
            byte[] md = mdInst.digest();
            // 把密文转换成十六进制的字符串形式
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    @CacheEvict(value = "project", key = "T(String).valueOf('project:').concat(#params['prjId'])")
    public int updatePrjDisable(ApolloMap<String, Object> params) throws Exception {
        int r = dao.update("updatePrjDisable", params);
        return r;
    }

    public int updatePrjShopDisable(ApolloMap<String, Object> params) throws Exception {
        int r = dao.update("updatePrjShopDisable", params);
        return r;
    }

    @CacheEvict(value = "project", key = "T(String).valueOf('project:').concat(#params['prjId'])")
    public int deleteProject(ApolloMap<String, Object> params) throws Exception {
        int r = dao.update("deletePrj", params);
        return r;
    }

    public int deleteProjectShop(ApolloMap<String, Object> params) throws Exception {
        int r = dao.update("deletePrjShop", params);
        return r;
    }

    public int getProjectInfoByName(Map params) throws Exception {
        Map<String, Object> retMap = new HashMap<String, Object>();
        PageInfo<ApolloRet> list = dao.findByPage(1, 1, "getProjectInfoByName", params);
        if (list.getSize() > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    public int getProjectInfoByNameAndId(Map params) throws Exception {
        Map<String, Object> retMap = new HashMap<String, Object>();
        PageInfo<ApolloRet> list = dao.findByPage(1, 1, "getProjectInfoByNameAndId", params);
        if (list.getSize() > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    public int getEnableShop(Map params) throws Exception {
        Map<String, Object> retMap = new HashMap<String, Object>();
        PageInfo<ApolloRet> list = dao.findByPage(1, 1, "getEnableShop", params);
        if (list.getSize() > 0) {
            return 1;
        } else {
            return 0;
        }
    }

	public List<ProjectVO> getSelectProjectsList(ProjectVO param) throws Exception {
		try {
			return dao.findAllList("pageSelectProject", param);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception();
		}
	}
	
	public List getProjectName(List idList) throws Exception {
		try {
			return dao.findAllList("getProjectName", idList);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception();
		}
	}
	
	/**
	 * 根据id获取1条projectvo
	 * @param prjId
	 * @return
	 * @throws Exception
	 */
	@Cacheable(value = "project", key = "T(String).valueOf('project:').concat(#prjId)",unless="#result==null")
    public ProjectVO findProjectById(String prjId) throws Exception {
        ProjectVO param = new ProjectVO();
        param.setPrjId(prjId);
        ProjectVO project = dao.findOne("pageSelectProject", param);
        return project;
    }
	
	public String getCurrentPrjDockingKingdee() throws Exception {
		Map<String, Object> paramsMap = new HashMap<String, Object>();
        CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
        String prjId = currentUser.getCurrentProject().getPrjId();
		paramsMap.put("prjId", prjId);
		PageInfo<ApolloRet> pageList = dao.findByPage(1, 1, "getProjectInfo", paramsMap);
		Map projectMap = pageList.getList().get(0);
		return projectMap.get("docking_kingdee").toString();
	}
	
}
